package com.yanqu.road.server.timer;

import com.yanqu.road.server.TempServer;
import com.yanqu.road.server.timer.banquet.*;
import com.yanqu.road.server.timer.dataanalytics.CrossDataAnalyticsTask;
import com.yanqu.road.server.timer.log.CrossAutoLogSaveTask;
import com.yanqu.road.server.timer.mogulbirthday.MogulBirthdayTask;
import com.yanqu.road.task.CheckDeadLockTask;
import com.yanqu.road.task.DirectMemoryMonitorTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 所有的定时器写在这边
 */
public class CenterBanquetTimerMgr {

    public static final int ALL_SYNC_TIME = 300000;      //5min全同步

    private static final Logger logger = LogManager.getLogger(CenterBanquetTimerMgr.class.getName());

    private static ScheduledExecutorService timerExecutorService;

    private static MgrDataSaveTask mgrDataSaveTask = null;

    private static ConnectionCheckTask connectionCheckTask = null;

    private static SyncBanquetTask syncBanquetTask = null;

    private static SyncBanquetMemberTask syncBanquetMemberTask = null;

    private static CrossDataAnalyticsTask crossDataAnalyticsTask = null;

    private static DirectMemoryMonitorTask directMemoryMonitorTask;
    private static SyncCrossFeastPartyTask syncCrossFeastPartyTask;
    private static CmdStatisticsTask cmdStatisticsTask;

    private static SyncJoinPartyTask syncJoinPartyTask = null;

    private static LogSaveTask logSaveTask;

    private static BanquetRobotsJoinTask banquetRobotsJoinTask = null;

    private static MogulBirthdayTask mogulBirthdayTask = null;

    private static CheckDeadLockTask checkDeadLockTask = null;

    private static CrossAutoLogSaveTask crossAutoLogSaveTask = null;

    public static synchronized boolean initGeneralTimers(int serverType) {
        timerExecutorService = TempServer.getTimerExecutorService();

        int interval = 5;
        if (null == mgrDataSaveTask) {
            mgrDataSaveTask = new MgrDataSaveTask();
            timerExecutorService.scheduleWithFixedDelay(mgrDataSaveTask, 0, 60, TimeUnit.SECONDS);
        }

        if (null == connectionCheckTask) {
            connectionCheckTask = new ConnectionCheckTask(serverType);
            timerExecutorService.scheduleWithFixedDelay(connectionCheckTask, 5, 5, TimeUnit.SECONDS);
        }


        if (null == syncBanquetTask) {
            syncBanquetTask = new SyncBanquetTask();
            timerExecutorService.scheduleWithFixedDelay(syncBanquetTask, 10, 5, TimeUnit.SECONDS);
        }

        if (null == syncBanquetMemberTask) {
            syncBanquetMemberTask = new SyncBanquetMemberTask();
            timerExecutorService.scheduleWithFixedDelay(syncBanquetMemberTask, 10, 1, TimeUnit.SECONDS);
        }

        if (null == crossDataAnalyticsTask){
            crossDataAnalyticsTask = new CrossDataAnalyticsTask();
            timerExecutorService.scheduleWithFixedDelay(crossDataAnalyticsTask, 0, 10, TimeUnit.SECONDS);
        }

        if (directMemoryMonitorTask == null) {
            directMemoryMonitorTask = new DirectMemoryMonitorTask();
            timerExecutorService.scheduleWithFixedDelay(directMemoryMonitorTask, 60, 60, TimeUnit.SECONDS);
        }

        if (syncCrossFeastPartyTask == null) {
            syncCrossFeastPartyTask = new SyncCrossFeastPartyTask();
            timerExecutorService.scheduleWithFixedDelay(syncCrossFeastPartyTask, 10, 30, TimeUnit.SECONDS);
        }

        if (syncJoinPartyTask == null) {
            syncJoinPartyTask = new SyncJoinPartyTask();
            timerExecutorService.scheduleWithFixedDelay(syncJoinPartyTask, 10, 1, TimeUnit.SECONDS);
        }

        if (cmdStatisticsTask == null) {
            cmdStatisticsTask = new CmdStatisticsTask();
            timerExecutorService.scheduleWithFixedDelay(cmdStatisticsTask, 60, 30, TimeUnit.SECONDS);
        }

        if(logSaveTask == null){
            logSaveTask = new LogSaveTask();
            timerExecutorService.scheduleWithFixedDelay(logSaveTask, 60, 60, TimeUnit.SECONDS);
        }

        if(null == banquetRobotsJoinTask){
            banquetRobotsJoinTask = new BanquetRobotsJoinTask();
            timerExecutorService.scheduleWithFixedDelay(banquetRobotsJoinTask, 60, 60, TimeUnit.SECONDS);
        }

        if(null == mogulBirthdayTask){
            mogulBirthdayTask = new MogulBirthdayTask();
            timerExecutorService.scheduleWithFixedDelay(mogulBirthdayTask, 60, 30, TimeUnit.SECONDS);
        }

        if (checkDeadLockTask == null) {
            checkDeadLockTask = new CheckDeadLockTask();
            timerExecutorService.scheduleWithFixedDelay(checkDeadLockTask, 0, 30, TimeUnit.SECONDS);
        }
        if (null == crossAutoLogSaveTask) {
            crossAutoLogSaveTask = new CrossAutoLogSaveTask();
            timerExecutorService.scheduleWithFixedDelay(crossAutoLogSaveTask, 60, 30, TimeUnit.SECONDS);
        }

        return true;
    }
}
